package tut.net.json.orgJson;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonParser {

	
	/*
	 	 {
			"id": 1,
		        "firstname": "Katerina",
			"languages": [
				{ "lang":"en" , "knowledge":"proficient" }, 
				{ "lang":"fr" , "knowledge":"advanced" }, 
			]
			"job":{
		                "site":"www.javacodegeeks.com",
		                "name":"Java Code Geeks",
		        }  
		} 
	 */
	
	//private static final String filePath = "C:\\Users\\dientn\\Desktop\\liveaid\\test data\\t.txt";
	private static final String filePath = "C:\\Users\\dientn\\Desktop\\liveaid\\test data\\test.txt";
	
	public static void main(String[] args) {

		try {
			// read the json file
			FileReader reader = new FileReader(filePath);

			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

			// get a String from the JSON object
			String firstName =  (String) jsonObject.get("api_version");
			System.out.println("api_version is: " + firstName);
			
			// result
			JSONObject result =  (JSONObject) jsonObject.get("result");
			System.out.println("result is: " + result.get("code") + ", with msg is: " + result.get("msg"));
			
			//account
			JSONObject account =  (JSONObject) jsonObject.get("account");
			System.out.println("account is: " + account.get("id") + ", with name is: " + account.get("name"));
			
			// request
			JSONObject request =  (JSONObject) jsonObject.get("request");
			System.out.println("request is: " + request.get("format") + ", with offset is: " + request.get("offset") + ", limit " + request.get("limit"));
			
			// report
			JSONObject report =  (JSONObject) jsonObject.get("report");
			JSONObject items = (JSONObject) report.get("items");
			JSONArray list = (JSONArray) items.get("item");
			
			System.out.println("------ Report -------");
			Iterator i = list.iterator();
			while (i.hasNext()) {
				JSONObject reportItem = (JSONObject) i.next();
				System.out.println("report Item  is: " + reportItem.get("adspot_id") + ",imp = "+ reportItem.get("imp") + ", click : " + reportItem.get("click"));
			}
			System.out.println("------ Report -------");
			
			
			//total
			JSONObject total =  (JSONObject) report.get("total");
			System.out.println("total is: " + total.get("count") + ", with imp is: " + total.get("imp"));
			
/*			// get a number from the JSON object
			long id =  (long) jsonObject.get("id");
			System.out.println("The id is: " + id);

			// get an array from the JSON object
			JSONArray lang= (JSONArray) jsonObject.get("languages");
			
			// take the elements of the json array
			for(int i=0; i<lang.size(); i++){
				System.out.println("The " + i + " element of the array: "+lang.get(i));
			}
			Iterator i = lang.iterator();

			// take each value from the json array separately
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				System.out.println("language "+ innerObj.get("lang") + 
						" with level " + innerObj.get("knowledge"));
			}
			// handle a structure into the json object
			JSONObject structure = (JSONObject) jsonObject.get("job");
			System.out.println("Into job structure, name: " + structure.get("name"));
*/
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ParseException ex) {
			ex.printStackTrace();
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		}

	}

}
